package gui;


import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class GUI extends JFrame implements ActionListener{
	private StartView startView;
	private DoctorView doctorView;
	private NurseView nurseView;
	private PatientView patientView;
	private AgencyView agencyView;
	private ImagePanel error, success;
	private AddRecordFrame addRecordFrame;
	protected ReadRecordFrame readRecordFrame;
	private EditRecordFrame editRecordFrame;
	private PatientChooserFrame patientChooser;
	private DeleteChooserFrame deleteChooser;
	private GUIControl gc;
	public GUI(GUIControl gc) {
		this.gc=gc;
		initViews();
		initExternalFrames();
		initFrame();
		
		this.setVisible(true);
		setView("AgencyView");
		
		this.pack();
	}
	public void setGuiVisible(boolean b){
		this.setVisible(b);
	}
	private void initExternalFrames() {
		addRecordFrame=new AddRecordFrame(gc);
		readRecordFrame=new ReadRecordFrame();
		editRecordFrame=new EditRecordFrame(gc);
		patientChooser=new PatientChooserFrame(gc);
		deleteChooser=new DeleteChooserFrame(gc); 
		//patientChooser.setVisible(true);
	}
	private void initViews() {
		startView=new StartView();
		doctorView=new DoctorView();
		nurseView=new NurseView();
		patientView= new PatientView();
		agencyView=new AgencyView();
		error=new ImagePanel("error.jpg");
		success=new ImagePanel("success.jpg");
		
		doctorView.addActionListeners(this);
		nurseView.addActionListeners(this);
		patientView.addActionListeners(this);
		agencyView.addActionListeners(this);
	}
	private void initFrame() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("Record Program");
	}
	public void setView(String mode) {
		this.getContentPane().setVisible(false);
		if (mode == "StartView") {
			this.getContentPane().setVisible(false);
			this.setContentPane(startView);
			startView.setVisible(true);
		}else if(mode == "DoctorView"){
			this.getContentPane().setVisible(false);
			this.setContentPane(doctorView);
			doctorView.setVisible(true);
		}else if(mode == "NurseView"){
			this.getContentPane().setVisible(false);
			this.setContentPane(nurseView);
			nurseView.setVisible(true);
		}else if(mode == "PatientView"){
			this.getContentPane().setVisible(false);
			this.setContentPane(patientView);
			patientView.setVisible(true);
		}else if(mode == "AgencyView"){
			this.getContentPane().setVisible(false);
			this.setContentPane(agencyView);
			agencyView.setVisible(true);
		}else if(mode == "Error"){
			this.getContentPane().setVisible(false);
			this.setContentPane(error);
			error.setVisible(true);
		}else if(mode == "Success"){
			this.getContentPane().setVisible(false);
			this.setContentPane(success);
			error.setVisible(true);
		}else{
			System.out.println("Fel i setView, mode var: "+mode);
		}
		this.pack();
	}
	@Override
	//doc klart m�ste kolla s� man kan l�sa och error bara
	//nurse klart utom respons, samma som doc
	//patient klar f�rutom att man m�ste skicka med patientnamnet som man vet via sitt cert
	//agency klar
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == "New Record") {
			addRecordFrame.setVisible(true);
			this.setEnabled(false);
			System.out.println("New Record pressed");
		}else if (e.getActionCommand() == "Read Record") {
			patientChooser.setVisible(true);
			this.setEnabled(false);
			System.out.println("Read Record pressed");
		}else if (e.getActionCommand() == "Edit Record") {
			editRecordFrame.setVisible(true);
			this.setEnabled(false);
			System.out.println("Edit Record pressed");
		}else if (e.getActionCommand() == "Read my record") { //fr�n patientView
			gc.readPatientsJournal();
			System.out.println("Read My Record pressed");
		}else if (e.getActionCommand() == "Delete Record") {//f�r agency
			deleteChooser.setVisible(true);
			this.setEnabled(false);
			System.out.println("Delete Record pressed");
		}
	}
}
